
 var signList=[{"signYear":"2016","signMonth":"06","signDay":"10"},{"signYear":"2016","signMonth":"05","signDay":"02"},{"signYear":"2016","signMonth":"05","signDay":"03"},{"signYear":"2016","signMonth":"06","signDay":"15"}];
  var signList_huikuan=[{"signYear":"2016","signMonth":"06","signDay":"10"},{"signYear":"2016","signMonth":"06","signDay":"07"},{"signYear":"2016","signMonth":"06","signDay":"08"},{"signYear":"2016","signMonth":"06","signDay":"20"}];
 
var calUtil = {
  //当前日历显示的年份
  showYear:2016,
  //当前日历显示的月份
  showMonth:3,
  //当前日历显示的天数
  showDays:1,
  eventName:"load",
  //初始化日历
  init:function(signList,signList_huikuan){
    calUtil.setMonthAndDay();
    calUtil.draw(signList,signList_huikuan);
    calUtil.bindEnvent();
  }, 
  draw:function(signList,signList_huikuan){
    //绑定日历
    var str = calUtil.drawCal(calUtil.showYear,calUtil.showMonth,signList,signList_huikuan);
    $("#calendar").html(str);
    //绑定日历表头
    var calendarName=calUtil.showYear+"年"+calUtil.showMonth+"月";
    $(".calendar_month_span").html(calendarName);  	
  },    
  //绑定事件
  bindEnvent:function(){
    //绑定上个月事件
    $(".calendar_month_prev").click(function(){
      //ajax获取日历json数据 
      calUtil.eventName="prev";
      calUtil.init(signList,signList_huikuan);
    });
	
    //绑定下个月事件
    $(".calendar_month_next").click(function(){
      //ajax获取日历json数据 
      calUtil.eventName="next";
      calUtil.init(signList,signList_huikuan);
    });
  },
  //获取当前选择的年月
  setMonthAndDay:function(){
    switch(calUtil.eventName)
    {
      case "load":
        var current = new Date();
        calUtil.showYear=current.getFullYear();
        calUtil.showMonth=current.getMonth() + 1;	
        break;	
      case "prev":
        var nowMonth=$(".calendar_month_span").html().split("年")[1].split("月")[0];
        calUtil.showMonth=parseInt(nowMonth)-1;
        if(calUtil.showMonth==0)
        {
          calUtil.showMonth=12;
          calUtil.showYear-=1;
        }
        break;
      case "next":
        var nowMonth=$(".calendar_month_span").html().split("年")[1].split("月")[0];
        calUtil.showMonth=parseInt(nowMonth)+1;
        if(calUtil.showMonth==13)
        {
            calUtil.showMonth=1;
            calUtil.showYear+=1;
        }
        break;
    }
  },
  getDaysInmonth : function(iMonth, iYear){
   var dPrevDate = new Date(iYear, iMonth, 0);
   return dPrevDate.getDate();
  },
  bulidCal : function(iYear, iMonth) {
   var aMonth = new Array();
   aMonth[0] = new Array(7);
   aMonth[1] = new Array(7);
   aMonth[2] = new Array(7);
   aMonth[3] = new Array(7);
   aMonth[4] = new Array(7);
   aMonth[5] = new Array(7);
   aMonth[6] = new Array(7);
   var dCalDate = new Date(iYear, iMonth - 1, 1);
   var iDayOfFirst = dCalDate.getDay();       
   var iDaysInMonth = calUtil.getDaysInmonth(iMonth, iYear); 
   var iVarDate = 1;
   var d, w;
   aMonth[0][0] = "日";
   aMonth[0][1] = "一";
   aMonth[0][2] = "二";
   aMonth[0][3] = "三";
   aMonth[0][4] = "四";
   aMonth[0][5] = "五";
   aMonth[0][6] = "六";
   for (d = iDayOfFirst; d < 7; d++) {
    aMonth[1][d] = iVarDate;
    iVarDate++;
   }
   for (w = 2; w < 7; w++) {
    for (d = 0; d < 7; d++) {
     if (iVarDate <= iDaysInMonth) {
      aMonth[w][d] = iVarDate;
      iVarDate++;
     }
    }
   }
   return aMonth;
  },
  ifHasSigned : function(signList,day){
   var signed = false;
   if(!signList){
     return signed;
   }
   $.each(signList,function(index,item){
    if( item.signYear == calUtil.showYear && item.signMonth == calUtil.showMonth && item.signDay == day  ) {
     signed = true;
     return false;
    }
   });
   return signed ;
  },
  ifHasSigned_huikuan : function(signList_huikuan,day){
  var Signed_huikuan = false;
  if(!signList_huikuan){
     return Signed_huikuan;
   }
  $.each(signList_huikuan,function(index,item){   
    if( item.signYear == calUtil.showYear && item.signMonth == calUtil.showMonth && item.signDay == day  ) {
     Signed_huikuan = true;
     return false;
    }
   });
   return Signed_huikuan;  
  },
  
  
  
  drawCal : function(iYear, iMonth ,signList,signList_huikuan) {
   var myMonth = calUtil.bulidCal(iYear, iMonth);
   var htmls = new Array();
   htmls.push("<div class='sign_main' id='sign_layer'>");
   htmls.push("<div class='sign_succ_calendar_title'>");
   htmls.push("<div class='gzh_qiandao_box1 tc fix f12 lh40 bbd ml10 mr10'><span class='db white   current-date auto'><span class='calendar_month_prev dib l mt13 poi'><img src='images/left.png'  /></span><div class='calendar_month_span dib g3'>2016年3月28日</div><span class='calendar_month_next dib r mt13 poi'><img src='images/right.png'  /></span></span></div");
 
   htmls.push("</div></div></div>");
   htmls.push("<div class='sign' id='sign_cal'>");
   htmls.push("<div class='fix gzh_qiandao_box4 auto bg_white'><ul  class='fix ul01 auto'><div ><div style='margin-right:-7px'><li  class='tc dib l bde'>日</li> <li  class='tc dib l bde'>一</li><li  class='tc dib l bde'>二</li><li  class='tc dib l bde'>三</li><li  class='tc dib l bde'>四</li><li  class='tc dib l bde'>五</li><li  class='tc dib l bde'>六</li>");   
	
	 htmls.push("</div></div></ul>");
	 htmls.push("<ul  class='fix ul02 auto'><div ><div style='margin-right:-7px' id='js-qiandao-list'>"); 
   var d, w;
   for (w = 1; w < 7; w++) {
  
    for (d = 0; d < 7; d++) {
     var ifHasSigned = calUtil.ifHasSigned(signList,myMonth[w][d]);
	   var ifHasSigned_huikuan = calUtil.ifHasSigned_huikuan(signList_huikuan,myMonth[w][d]);

     if(ifHasSigned ==true  && ifHasSigned_huikuan == false){
      htmls.push("<li class='qiandao rel   tc dib l bde  date1" +myMonth[w][d] + "'>" + (!isNaN(myMonth[w][d]) ? myMonth[w][d] : " ") + "<div class='qiandao-icon abs'></div></li>");
     }
	   else if(ifHasSigned == false && ifHasSigned_huikuan == true ){
      htmls.push("<li class='huikuan  rel   tc dib l bde  date1" +myMonth[w][d] + "'>" + (!isNaN(myMonth[w][d]) ? myMonth[w][d] : " ") + "<div class='qiandao-icon abs'></div></li>");
     }
	   else if(ifHasSigned ==true  && ifHasSigned_huikuan == true){
      htmls.push("<li class='huikuan_qiandao  rel   tc dib l bde  date1" +myMonth[w][d] + "'>" + (!isNaN(myMonth[w][d]) ? myMonth[w][d] : " ") + "<div class='qiandao-icon abs'></div></li>");
     }   
	   else {
      htmls.push("<li class='  tc dib l bde rel " +myMonth[w][d] + "'>" + (!isNaN(myMonth[w][d]) ? myMonth[w][d] : " ") + "<div class='qiandao-icon abs'></div></li>");
     }	 
    }   
   }
   htmls.push("</div></div></ul>");
   return htmls.join('');
  }
  
};	
$(".js-just-qiandao").click(function(){	
	var current = new Date();
	if(calUtil.showYear==current.getFullYear() && calUtil.showMonth==current.getMonth() + 1){    
     $("." + current.getDate()).addClass('qiandao'); 
  }
  $(".js-just-qiandao span").html("已签到");
  signList.push({signYear:current.getFullYear(),signMonth:current.getMonth()+1,"signDay":current.getDate()}); 
}); 
	

